package com.schoolexam.controller;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.CircleCaptcha;
import cn.hutool.core.util.IdUtil;
import com.schoolexam.utils.RedisClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@RestController
//@RequestMapping("/send")
public class CaptureController {

    @Autowired
    private RedisClient redisClient;

    @GetMapping("/captcha")
    public void getCaptcha(HttpServletResponse response) {
        //生成验证码
        CircleCaptcha captcha = CaptchaUtil.createCircleCaptcha(120, 48, 4, 2);
        String captchaCode  = captcha.getCode();

        String captchaKey  = IdUtil.fastSimpleUUID();
        redisClient.set(captchaKey, captchaCode);
        redisClient.expire(captchaKey, 300);
        // 返回验证码图片
        response.setContentType("image/png");
        try {
            captcha.write(response.getOutputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
